const moment = require('moment-timezone');
const U = {
  getTimeZone() {
    return 'UTC';
  },

  toUTC(source, format = 'YYYY-MM-DD HH:mm:ss') {
    if (source instanceof Date && !isNaN(source.getTime())) {
      return this.getDateToUTC(source);
    } else if (moment(source, format, true).isValid()) {
      return this.getTimeStrToUTC(source);
    } else if (this.isTimestamp(source)) {
      return this.getTimeStampToUTC(source);
    }
  },

  isTimestamp(value) {
    const num = Number(value);
    return !isNaN(num) && this.isValidTimestamp(num);
  },

  isValidTimestamp(value) {
    if (!Number.isFinite(value)) {
      return false;
    }
    const date = new Date(value);
    return !isNaN(date.getTime());
  },

  //
  getDateToUTC(dataParam) {
    return moment.tz(dataParam, null, this.getTimeZone()).clone().utc();
  },

  getTimeStrToUTC(dataParam) {
    return moment.tz(dataParam, null, this.getTimeZone()).clone().utc();
  },

  getTimeStampToUTC(dataParam) {
    // 时间戳是
    const timestampStr = dataParam.toString();
    if (timestampStr.length === 10) {
      dataParam = dataParam * 1000;
    }
    return moment
      .tz(dataParam, null, this.getTimeZone())
      .clone()
      .utc()
      .valueOf();
  },
};

let arrTmp = [
  U.toUTC('2014-04-18 23:40:00'),
  U.toUTC(new Date('2014-04-18 23:40:00')),
  U.toUTC(new Date('2014-04-18 23:40:00').valueOf()),
  moment
    .tz(U.toUTC('1397835600'), null, U.getTimeZone())
    .format('YYYY-MM-DD HH:mm:ss'),
];

console.log('arrTmp', arrTmp);
